class Solution {
public:
    int dfs(TreeNode* root, int sum) {
        int x = root->val;
        sum = sum * 10 + x;
        if (root->left == nullptr && root->right == nullptr)return sum;
        int ans = 0;
        if (root->left) ans += dfs(root->left, sum);
        if (root->right) ans += dfs(root->right, sum);
        return ans;
    }
    int sumNumbers(TreeNode* root) {
        int ans = 0;
        ans = dfs(root, 0);
        return ans;
    }
};